package com.appkudos.controller;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.appkudos.model.App;
import com.appkudos.model.Group;
import com.appkudos.model.Recommendation;
import com.appkudos.model.User;


public class UserController extends Controller {
   String response = "";
	String output = "";
	ArrayList<NameValuePair> nameValuePairs;
	
	public UserController(){
		
	}
	
	/***
	 * Add app to user's installed apps.  
	 * @param username
	 * @param appid
	 * @return true if successful operation
	 */
	public boolean addApp(String username, String appid, String name, String desc) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","addApp"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    nameValuePairs.add(new BasicNameValuePair("appid",appid));
		    nameValuePairs.add(new BasicNameValuePair("appname",name));
		    nameValuePairs.add(new BasicNameValuePair("appdesc",desc));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    
		    return executeOperation(response);
	}
	
	/***
	 * Add a new app to user's installed apps. Also, check if the app is in 
	 * app table.
	 * @param username
	 * @param appid
	 * @return true if successful operation
	 */
	public boolean addNewApp(String username, String appid, String name, String desc) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","addNewApp"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    nameValuePairs.add(new BasicNameValuePair("appid",appid));
		    nameValuePairs.add(new BasicNameValuePair("appname",name));
		    nameValuePairs.add(new BasicNameValuePair("appdesc",desc));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    
		    return executeOperation(response);
	}
	
	/***
	 * Add a list of new apps to both the Apps table and User's installed apps.
	 * @param username
	 * @param List<App>
	 * @return true if successful operation
	 */
	public boolean addNewApps(String username, List<App> appslist) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    
		    JSONObject jsonapplist = new JSONObject();
		    try {
				 for(int i = 0; i < appslist.size(); i++){
					// Make an inner JSON object for each app
					JSONObject jsonapp = new JSONObject();
				   	jsonapp.put("appid", appslist.get(i).getAppid());
		    		jsonapp.put("name", appslist.get(i).getAppName());
		    		jsonapp.put("desc", appslist.get(i).getAppDesc());
		    		
		    		// Wrap in outer JSON object for list of apps
		    		jsonapplist.put("App"+i, jsonapp);		
				 }
		    } catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    nameValuePairs.add(new BasicNameValuePair("function","addNewApps"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    nameValuePairs.add(new BasicNameValuePair("JSONapplist",jsonapplist.toString()));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    
		    return executeOperation(response);
	}
	
	/***
	 * Add a list of apps to User's installed apps.
	 * @param username
	 * @param List<App>
	 * @return true if successful operation
	 */
	public boolean addApps(String username, List<App> appslist) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    
		    JSONObject jsonapplist = new JSONObject();
		    try {
				 for(int i = 0; i < appslist.size(); i++){
					// Make an inner JSON object for each app
					JSONObject jsonapp = new JSONObject();
				   	jsonapp.put("appid", appslist.get(i).getAppid());
		    		
		    		// Wrap in outer JSON object for list of apps
		    		jsonapplist.put("App"+i, jsonapp);		
				 }
		    } catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    nameValuePairs.add(new BasicNameValuePair("function","addApps"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    nameValuePairs.add(new BasicNameValuePair("JSONapplist","{"+jsonapplist.toString()+"}"));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    
		    return executeOperation(response);
	}	
	
	/***
	 * Remove app
	 * @param username
	 * @param appid
	 * @return true if successful operation
	 */
	public boolean removeApp(String username, String appid) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","removeApp"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    nameValuePairs.add(new BasicNameValuePair("appid",appid));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    
		    return executeOperation(response);
	}
	
	/***
	 * Create a new user
	 * @param username
	 * @param firstname
	 * @param lastname
	 * @return true if successful operation
	 */
	public boolean createUser(String username, String firstname, String lastname) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","createUser"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    nameValuePairs.add(new BasicNameValuePair("firstname",firstname));
		    nameValuePairs.add(new BasicNameValuePair("lastname",lastname));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    
		    return executeOperation(response);
	}
	
	/***
	 * Add a friend for a user
	 * @param username1
	 * @param username2
	 * @return true if successful operation
	 */
	public boolean addFriend(String username1, String username2) {
			nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","addFriend"));
		    nameValuePairs.add(new BasicNameValuePair("username1",username1));
		    nameValuePairs.add(new BasicNameValuePair("username2",username2));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    
		    return executeOperation(response);
	}	
	
	/***
	 * Add a user to a group
	 * @param username
	 * @param groupid
	 * @return true if successful operation
	 */
	public boolean addUserToGroup(String username, String groupid) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","joinGroup"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    nameValuePairs.add(new BasicNameValuePair("groupid",groupid));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    
		    return executeOperation(response);
	}	
	
	/***
	 * Remove a user from a group
	 * @param username
	 * @param groupid
	 * @return true if successful operation
	 */
	public boolean removeUserFromGroup(String username, String groupid) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","leaveGroup"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    nameValuePairs.add(new BasicNameValuePair("groupid",groupid));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    
		    return executeOperation(response);
	}	
	
	
	/***
	 * Gets all info for this user
	 * @param username
	 * @return User
	 */
	public User getUserProfile(String username) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","getUserProfile"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    User returnUser = null;
		    
		    //parse json data
		    try{
		    	JSONArray jArray = new JSONArray("["+response+"]");
	    		for(int i=0;i<jArray.length();i++){
	            	JSONObject json_data = jArray.getJSONObject(i);
	            	if(json_data.has("ERROR")){
	            		//output += json_data.getString("ERROR");
	            	}else{
	            		returnUser = new User(json_data.getString("username"), 
	            				json_data.getString("firstname"), json_data.getString("lastname"),
	            				json_data.getString("dateCreated"));
		            }
	            }
		    }catch(JSONException e){
		    		returnUser = new User(e.toString(),e.toString(),"");
		    }
		   	  
		    return returnUser;
	}		
	
	
	
	/***
	 * Gets all friends of this user
	 * @param username
	 * @return List<User> who this user is friends with
	 */
	public List<User> getFriendsOfUser(String username) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","getFriendsUser"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    List<User> returnUsers = new ArrayList<User>();
		    
		    //parse json data
		    try{
		    	JSONArray jArray = new JSONArray("["+response+"]");
	    		for(int i=0;i<jArray.length();i++){
	            	JSONObject json_data = jArray.getJSONObject(i);
	            	if(json_data.has("ERROR")){
	            		//output += json_data.getString("ERROR");
	            	}else{
	            		returnUsers.add(new User(json_data.getString("username"), 
	            				json_data.getString("firstname"), json_data.getString("lastname")));
		            }
	            }
		    }catch(JSONException e){
		    	returnUsers.add(new User(e.toString(), 
        				"",""));
		    }
		   	  
		    return returnUsers;
	}		
	
	/***
	 * Gets all groups this user has joined
	 * @param username
	 * @return List<Group> this user is a member of
	 */
	public List<Group> getGroupsOfUser(String username) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","getMyGroups"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    List<Group> returnGroups = new ArrayList<Group>();
		    
		    //parse json data
		    try{
		    	JSONArray jArray = new JSONArray("["+response+"]");
	    		for(int i=0;i<jArray.length();i++){
	            	JSONObject json_data = jArray.getJSONObject(i);
	            	if(json_data.has("ERROR")){
	            		//output += json_data.getString("ERROR");
	            	}else{
	            		returnGroups.add(new Group(json_data.getString("groupid"), 
	            				json_data.getString("name"), json_data.getString("desc")));
		            }
	            }
		    }catch(JSONException e){
		    		
		    }
		   	  
		    return returnGroups;
	}		
	
	/***
	 * Gets all apps installed by this user
	 * @param username
	 * @return List<App> 
	 */
	public List<App> getAppsForUser(String username) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","getAppsByUser"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    List<App> returnApps = new ArrayList<App>();
		    
		    //parse json data
		    try{
		    	JSONArray jArray = new JSONArray("["+response+"]");
	    		for(int i=0;i<jArray.length();i++){
	            	JSONObject json_data = jArray.getJSONObject(i);
	            	if(json_data.has("ERROR")){
	            		//returnApps.add(new App("ERROR","ERROR","ERROR"));
	            		//output += json_data.getString("ERROR");
	            	}else{
	            		returnApps.add(new App(json_data.getString("appid"), 
	            				json_data.getString("name"), json_data.getString("desc"), 
	            				json_data.getString("dateInstalled"), 
	            				json_data.getString("IsFavorite").equals("1"), true));
		            }
	            }
		    }catch(JSONException e){
		    	//returnApps.add(new App("ERROR2","ERROR2","ERROR2","ERROR2",false,true));
		    }
		   	  
		    return returnApps;
	}			
	
	/***
	 * Gets all apps installed by this user in the past 30 days
	 * @param username
	 * @return List<App> 
	 */
	public List<App> getRecentAppsForUser(String username) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","getRecentApps"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    List<App> returnApps = new ArrayList<App>();
		    
		    //parse json data
		    try{
		    	JSONArray jArray = new JSONArray("["+response+"]");
	    		for(int i=0;i<jArray.length();i++){
	            	JSONObject json_data = jArray.getJSONObject(i);
	            	if(json_data.has("ERROR")){
	            		//output += json_data.getString("ERROR");
	            	}else{
	            		returnApps.add(new App(json_data.getString("appid"), 
	            				json_data.getString("name"), json_data.getString("desc"), 
	            				json_data.getString("dateInstalled"), 
	            				json_data.getString("isFavorite").equals("1"), true));
		            }
	            }
		    }catch(JSONException e){
		    		
		    }
		   	  
		    return returnApps;
	}			
	
	/***
	 * Mark an app as favorite for this user
	 * @param username
	 * @param appid
	 * @return true if successful operation
	 */
	public boolean addFavApp(String username, String appid) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","addFavApp"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    nameValuePairs.add(new BasicNameValuePair("appid",appid));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    
		    return executeOperation(response);
	}
	
	
	/***
	 * Gets all apps installed by this user and marked as favorite
	 * @param username
	 * @return List<App> 
	 */
	public List<App> getFavAppsForUser(String username) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","retrieveFavApps"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    List<App> returnApps = new LinkedList<App>();
		    
		    //parse json data
		    try{
		    	JSONArray jArray = new JSONArray("["+response+"]");
	    		for(int i=0;i<jArray.length();i++){
	            	JSONObject json_data = jArray.getJSONObject(i);
	            	if(json_data.has("ERROR")){
	            		//output += json_data.getString("ERROR");
	            	}else{
	            		returnApps.add(new App(json_data.getString("appid"), 
	            				json_data.getString("name"), json_data.getString("desc"), 
	            				json_data.getString("dateInstalled"), true, true));
		            }
	            }
		    }catch(JSONException e){
		    		
		    }
		   	  
		    return returnApps;
	}			
	
	/***
	 * Gets all apps not installed by this user
	 * @param username
	 * @return List<App> 
	 */
	public List<App> getAppsNotInstalledForUser(String username) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","getNotInstalledApp"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    List<App> returnApps = new ArrayList<App>();
		    
		    //parse json data
		    try{
		    	JSONArray jArray = new JSONArray("["+response+"]");
	    		for(int i=0;i<jArray.length();i++){
	            	JSONObject json_data = jArray.getJSONObject(i);
	            	if(json_data.has("ERROR")){
	            		//output += json_data.getString("ERROR");
	            	}else{
	            		returnApps.add(new App(json_data.getString("appid"), 
	            				json_data.getString("name"), json_data.getString("desc"), 
	            				"", false, false));
		            }
	            }
		    }catch(JSONException e){
		    		
		    }
		   	  
		    return returnApps;
	}			
	/***
	 * Gets all apps not installed by this user
	 * @param username
	 * @return List<App> 
	 */
	public List<App> getAppsInDB() {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","getAppsInDB"));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    List<App> returnApps = new ArrayList<App>();
		    
		    //parse json data
		    try{
		    	JSONArray jArray = new JSONArray("["+response+"]");
	    		for(int i=0;i<jArray.length();i++){
	            	JSONObject json_data = jArray.getJSONObject(i);
	            	if(json_data.has("ERROR")){
	            		//output += json_data.getString("ERROR");
	            	}else{
	            		returnApps.add(new App(json_data.getString("appid"), 
	            				json_data.getString("name"), json_data.getString("desc"), 
	            				"", false, false));
		            }
	            }
		    }catch(JSONException e){
		    		
		    }
		   	  
		    return returnApps;
	}		
	/***
	 * Unmark an app as favorite for this user
	 * @param username
	 * @param appid
	 * @return true if successful operation
	 */
	public boolean removeFavApp(String username, String appid) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","removeFavApp"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    nameValuePairs.add(new BasicNameValuePair("appid",appid));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    
		    return executeOperation(response);
	}

	
	/***
	 * Gets all recs made to or from this user, sorted by most recent date sent
	 * @param username
	 * @return List<Rec> for recommendations sent to this user
	 */
	public List<Recommendation> getRecsForUser(String username) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","getRecsForUser"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    List<Recommendation> returnRecs = new ArrayList<Recommendation>();
		    
		    //parse json data
		    try{
		    	JSONArray jArray = new JSONArray("["+response+"]");
	    		for(int i=0;i<jArray.length();i++){
	            	JSONObject json_data = jArray.getJSONObject(i);
	            	if(json_data.has("ERROR")){
	            		//output += json_data.getString("ERROR");
	            	}else{
	            		returnRecs.add(new Recommendation(json_data.getString("recid"), 
	            				json_data.getString("message"), json_data.getString("sender"),
	            				json_data.getString("recipient"), json_data.getString("dateSent"),
	            				json_data.getString("isGroupReceipient").equals("1"), json_data.getString("appid")));
		            }
	            }
		    }catch(JSONException e){
		    		
		    }
		   	  
		    return returnRecs;
	}			
	
	/***
	 * Gets all recs made to this user, sorted by most recent date sent
	 * @param username
	 * @return List<Rec> for recommendations sent to this user
	 */
	public List<Recommendation> getRecsToUser(String username) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","getRecsToUser"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    List<Recommendation> returnRecs = new ArrayList<Recommendation>();
		    
		    //parse json data
		    try{
		    	JSONArray jArray = new JSONArray("["+response+"]");
	    		for(int i=0;i<jArray.length();i++){
	            	JSONObject json_data = jArray.getJSONObject(i);
	            	if(json_data.has("ERROR")){
	            		//output += json_data.getString("ERROR");
	            	}else{
	            		returnRecs.add(new Recommendation(json_data.getString("recid"), 
	            				json_data.getString("message"), json_data.getString("sender"),
	            				username, json_data.getString("dateSent"),
	            				json_data.getString("isGroupReceipient").equals("1"), json_data.getString("appid")));
		            }
	            }
		    }catch(JSONException e){
		    		
		    }
		   	  
		    return returnRecs;
	}			
	
	/***
	 * Gets all recs sent from this user, sorted by most recent date sent
	 * @param username
	 * @return List<Rec> for recommendations this user sent
	 */
	public List<Recommendation> getRecsFromUser(String username) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","getRecsFromUser"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    List<Recommendation> returnRecs = new ArrayList<Recommendation>();
		    
		    //parse json data
		    try{
		    	JSONArray jArray = new JSONArray("["+response+"]");
	    		for(int i=0;i<jArray.length();i++){
	            	JSONObject json_data = jArray.getJSONObject(i);
	            	if(json_data.has("ERROR")){
	            		//output += json_data.getString("ERROR");
	            	}else{
	            		returnRecs.add(new Recommendation(json_data.getString("recid"), 
	            				json_data.getString("message"), username,
	            				json_data.getString("recipient"), json_data.getString("dateSent"),
	            				json_data.getString("isGroupReceipient").equals("1"), json_data.getString("appid")));
		            }
	            }
		    }catch(JSONException e){
		    		
		    }
		   	  
		    return returnRecs;
	}		
	
	/***
	 * Gets 5 most recent recs sent to or from this user
	 * @param username
	 * @return List<Rec> for recommendations this user sent
	 */
	public List<Recommendation> getRecentRecsForUser(String username) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","getRecentRecsForUser"));
		    nameValuePairs.add(new BasicNameValuePair("username",username));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    List<Recommendation> returnRecs = new ArrayList<Recommendation>();
		    
		    //parse json data
		    try{
		    	JSONArray jArray = new JSONArray("["+response+"]");
	    		for(int i=0;i<jArray.length();i++){
	            	JSONObject json_data = jArray.getJSONObject(i);
	            	if(json_data.has("ERROR")){
	            		//output += json_data.getString("ERROR");
	            	}else{
	            		returnRecs.add(new Recommendation(json_data.getString("recid"), 
	            				json_data.getString("message"), 
	            				json_data.getString("sender"),
	            				json_data.getString("recipient"), json_data.getString("dateSent"),
	            				json_data.getString("isGroupReceipient").equals("1"), json_data.getString("appid")));
		            }
	            }
		    }catch(JSONException e){
		    		
		    }
		   	  
		    return returnRecs;
	}			
	
	/***
	 * Gets all users who match the input keyword
	 * @param keyword
	 * @return List<User> who match the keyword
	 */
	public List<User> searchForUser(String keyword) {
		    nameValuePairs = new ArrayList<NameValuePair>();
		    nameValuePairs.add(new BasicNameValuePair("function","searchForUser"));
		    nameValuePairs.add(new BasicNameValuePair("keyword",keyword));
		    
		    response = sendData(URL_USER_QUERIES, nameValuePairs);
		    List<User> returnUsers = new ArrayList<User>();
		    
		    //parse json data
		    try{
		    	JSONArray jArray = new JSONArray("["+response+"]");
	    		for(int i=0;i<jArray.length();i++){
	            	JSONObject json_data = jArray.getJSONObject(i);
	            	if(json_data.has("ERROR")){
	            		//output += json_data.getString("ERROR");
	            	}else{
	            		returnUsers.add(new User(json_data.getString("username"), 
	            				json_data.getString("firstname"), json_data.getString("lastname"),
	            				json_data.getString("dateCreated")));
		            }
	            }
		    }catch(JSONException e){
		    		
		    }
		   	  
		    return returnUsers;
	}			
	
	public List<User> getAllUsers() {
	    nameValuePairs = new ArrayList<NameValuePair>();
	    nameValuePairs.add(new BasicNameValuePair("function","getAllUsers"));
	    
	    response = sendData(URL_USER_QUERIES, nameValuePairs);
	    List<User> returnUsers = new ArrayList<User>();
	    
	    //parse json data
	    try{
	    	JSONArray jArray = new JSONArray("["+response+"]");
    		for(int i=0;i<jArray.length();i++){
            	JSONObject json_data = jArray.getJSONObject(i);
            	if(json_data.has("ERROR")){
            		//output += json_data.getString("ERROR");
            	}else{
            		returnUsers.add(new User(json_data.getString("username"), 
            				json_data.getString("firstname"), json_data.getString("lastname")));
	            }
            }
	    }catch(JSONException e){
	    		
	    }
	   	  
	    return returnUsers;
	}
	
	
	public Intent passCurrentUser(Intent intent, User user){
        intent.putExtra("username", user.getUsername());
        intent.putExtra("firstname", user.getFirstname());
        intent.putExtra("lastname", user.getLastname());     
		return intent;
	}
	
	/**
	 * current user in the session
	 * @param extras
	 * @return
	 */
	public User getCurrentUser(Bundle extras){
        User user = null;
		if(extras !=null)
        {
        	String username = extras.getString("username");
        	String firstname = extras.getString("firstname");
        	String lastname = extras.getString("lastname");
        	user = new User(username, firstname, lastname);   	
        }
		return user;
	}
	
	public Intent passFriend(Intent intent, User user){
        intent.putExtra("fusername", user.getUsername());
        intent.putExtra("ffirstname", user.getFirstname());
        intent.putExtra("flastname", user.getLastname());     
		return intent;
	}
	
	/**
	 * current user in the session
	 * @param extras
	 * @return
	 */
	public User getFriend(Bundle extras){
        User user = null;
		if(extras !=null)
        {
			if(extras.getString("fusername") != null){
	        	String username = extras.getString("fusername");
	        	String firstname = extras.getString("ffirstname");
	        	String lastname = extras.getString("flastname");
	        	user = new User(username, firstname, lastname);  
        	} 	
        }
		return user;
	}
	
	public Intent passCurrentGroup(Intent intent, Group group){
        intent.putExtra("gid", group.getGroupID());
        intent.putExtra("gname", group.getGroupName());
        intent.putExtra("gdesc", group.getGroupDesc());     
		return intent;
	}
	
	/**
	 * current group in the session
	 * @param extras
	 * @return
	 */
	public Group getCurrentGroup(Bundle extras){
        Group group = null;
		if(extras !=null)
        {
			if(extras.getString("gid") != null){
	        	String gid = extras.getString("gid");
	        	String gname = extras.getString("gname");
	        	String gdesc = extras.getString("gdesc");
	        	group = new Group(gid, gname, gdesc);   
        	}	
        }
		return group;
	}
	
	public Intent passApp(Intent intent, App app){
        intent.putExtra("appid", app.getAppid());
        intent.putExtra("appdesc", app.getAppDesc());
        intent.putExtra("appname", app.getAppName());
    
		return intent;
	}
	
	
	/**
	 * current group in the session
	 * @param extras
	 * @return
	 */
	public App getApp(Bundle extras){
        App app = null;
		if(extras !=null)
        {
			if(extras.getString("appid") != null){
	        	String appid = extras.getString("appid");
	        	String appname = extras.getString("appname");
	        	String appdesc = extras.getString("appdesc");
	        	app = new App(appid, appname, appdesc);   
        	}	
        }
		return app;
	}
	
	public Intent passRec(Intent intent, Recommendation rec){
        intent.putExtra("recid", rec.getRecid());
        intent.putExtra("sender", rec.getSender());
        intent.putExtra("receiver",rec.getReceiver() );
        intent.putExtra("msg", rec.getMessage());
        intent.putExtra("isGroup",  new Boolean(rec.isGroupRecipient()).toString());
        intent.putExtra("isSent", rec.getDateSent());
        intent.putExtra("appid", rec.getAppid());
		return intent;
	}
	
	
	/**
	 * current recommendation in the session
	 * @param extras
	 * @return
	 */
	public Recommendation getRec(Bundle extras){
        Recommendation rec = null;
        
		if(extras !=null)
        {
	        	String recid = extras.getString("recid");
	        	String sender = extras.getString("sender");
	        	String receiver = extras.getString("receiver");
	        	String msg = extras.getString("msg");
	        	String isGroup = extras.getString("isGroup");
	        	String isSent = extras.getString("isSent");
	        	rec = new Recommendation (recid, msg, sender, receiver, isSent);  
        }
		return rec;
	}
	
	/**
	 * Gets information of appid
	 * @param username, appid
	 * @return App
	 */
	public App getAppForUser(String appid) {
		nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("function","getAppInfo"));
		nameValuePairs.add(new BasicNameValuePair("appid",appid));

		response = sendData(URL_USER_QUERIES, nameValuePairs);
		App returnApp = null;
	
		//parse json data
		try {
			JSONArray jArray = new JSONArray("["+response+"]");
			for(int i=0;i<jArray.length();i++){
				JSONObject json_data = jArray.getJSONObject(i);
				if(json_data.has("ERROR")) {
					//returnApps.add(new App("ERROR","ERROR","ERROR"));
					//output += json_data.getString("ERROR");
				}
				else {
					returnApp = new App(appid, json_data.getString("name"),
							json_data.getString("desc"));
				}
			}
		}
	 	catch(JSONException e){
		 	//returnApps.add(new App("ERROR2","ERROR2","ERROR2","ERROR2",false,true));
	 	}
	 
	 	return returnApp;
	}
}